#include "animationeditor.h"
#include "ui_animationeditor.h"

AnimationEditor::AnimationEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AnimationEditor)
{
    ui->setupUi(this);
    ui->view->AddShape(&_shape);
    _shape.SetFillColor(sf::Color::Transparent);
    _shape.SetOutlineColor(sf::Color::Red);
    _shape.SetOutlineThickness(2.f);

    connect(ui->width,   SIGNAL(valueChanged(int)), this, SLOT(UpdateShape()));
    connect(ui->height,  SIGNAL(valueChanged(int)), this, SLOT(UpdateShape()));
    connect(ui->offsetX, SIGNAL(valueChanged(int)), this, SLOT(UpdateShape()));
    connect(ui->offsetY, SIGNAL(valueChanged(int)), this, SLOT(UpdateShape()));
    connect(ui->inLoop,  SIGNAL(stateChanged(int)), this, SLOT(UpdateAnimation()));
    connect(ui->refresh, SIGNAL(valueChanged(int)), this, SLOT(UpdateAnimation()));
    connect(ui->frames,  SIGNAL(valueChanged(int)), this, SLOT(UpdateAnimation()));

    connect(ui->listTextures, SIGNAL(currentIndexChanged(int)), this, SLOT(ShowTexture()));
    connect(ui->listTextures, SIGNAL(currentIndexChanged(int)), this, SLOT(ChangeTexture()));

    connect(ui->buttonPlay, SIGNAL(clicked()), this, SLOT(ShowAnimation()));
    connect(ui->buttonStop, SIGNAL(clicked()), this, SLOT(ShowTexture()));

    QIcon iconPlay("icons/anim-play.png");
    QIcon iconStop("icons/anim-pause.png");

    ui->buttonPlay->setIcon(iconPlay);
    ui->buttonStop->setIcon(iconStop);

    _loading = true;
    this->setEnabled(false);
}

AnimationEditor::~AnimationEditor()
{
    delete ui;
}

void AnimationEditor::SetTextureManager(TextureManager *tm)
{
    _textureManager = tm;
    connect(_textureManager, SIGNAL(TextureAdded(QString)), this, SLOT(TextureAdded(QString)));
}

void AnimationEditor::TextureAdded(QString name)
{
    ui->listTextures->addItem(name);
}

void AnimationEditor::ChangeTexture()
{
    if (_loading)
        return ;
    sf::Texture* tex = _textureManager->GetByName(ui->listTextures->currentText());

    if (tex)
      _animation->SetTexture(tex);
}

void AnimationEditor::ShowTexture()
{
    const QString      name = ui->listTextures->currentText();
    const sf::Texture* tex  = _textureManager->GetByName(name);

    if (tex)
      ui->view->SetTexture(*tex);
    _shape.SetOutlineThickness(2.f);
}

void AnimationEditor::ShowAnimation()
{
    Shiny::SpriteAnimate&                sprite = ui->view->GetSpriteAnimate();
    Shiny::SpriteAnimate::AnimationArray array;

    array.push_back(*_animation);
    sprite.SetAnimations(array);
    _shape.SetOutlineThickness(0.f);
}

void AnimationEditor::UpdateShape()
{
    if (_loading)
        return ;
    sf::Vector2f size;
    sf::Vector2f position;

    size.x     = ui->width->value();
    size.y     = ui->height->value();
    position.x = ui->offsetX->value();
    position.y = ui->offsetY->value();
    _shape.SetSize(size);
    _shape.SetPosition(position);
    UpdateAnimation();
}

void AnimationEditor::UpdateAnimation()
{
    if (_loading)
        return ;
    sf::Texture* tex = _textureManager->GetByName(ui->listTextures->currentText());
    sf::IntRect  rect;

    rect.Height = ui->height->value();
    rect.Width  = ui->width->value();
    rect.Left   = ui->offsetX->value();
    rect.Top    = ui->offsetY->value();
    _animation->SetInLoop(ui->inLoop->isChecked());
    _animation->SetRefreshRate(ui->refresh->value());
    _animation->SetNFrames(ui->frames->value());
    _animation->SetRect(rect);
    _animation->SetTexture(tex);
    _animationData->textureName = ui->listTextures->currentText();
}

void AnimationEditor::LoadAnimation(QString name)
{
    _loading   = true;
    _animationData = _animationManager->GetDataByName(name);
    _animation     = &_animationData->second;

    sf::IntRect rect = _animation->Rect();

    ui->height->setValue(rect.Height);
    ui->width->setValue(rect.Width);
    ui->offsetX->setValue(rect.Left);
    ui->offsetY->setValue(rect.Top);
    ui->inLoop->setChecked(_animation->IsInLoop());
    ui->frames->setValue(_animation->NFrames());
    ui->refresh->setValue(_animation->RefreshRate());

    for (int i = 0 ; i < ui->listTextures->count() ; ++i)
    {
	const sf::Texture* tex = _textureManager->GetByName(ui->listTextures->itemText(i));

	if (tex == _animation->Texture())
	{
	    ui->listTextures->setCurrentIndex(i);
	    break ;
	}
    }

    sf::Vector2f size;
    sf::Vector2f position;

    size.x     = ui->width->value();
    size.y     = ui->height->value();
    position.x = ui->offsetX->value();
    position.y = ui->offsetY->value();
    _shape.SetSize(size);
    _shape.SetPosition(position);

    _loading   = false;
    this->setEnabled(true);
}
